/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.example.androidlearing2423.ui.theme.act.ui

import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.androidlearing2423.R
import com.example.androidlearing2423.ui.theme.theme.AndroidLearing2423Theme
import androidx.annotation.StringRes // 新增：引入StringRes注解
import androidx.annotation.DrawableRes // 新增：引入DrawableRes注解
import com.example.androidlearing2423.ui.theme.theme.AndroidLearing2423Theme

// 定义微信消息数据类
data class WeChatMsg(
    val avatar: Int,
    val name: String,
    val content: String,
    val time: String
)

// 定义Activity入口数据类
data class EntryBean(
    val activityName: String,
    val description: String,
    val date: String,
    val activityClass: Class<*>
)

class Datasource() {
    fun loadAffirmations(): List<Affirmation> {
        return listOf<Affirmation>(
            Affirmation(R.string.affirmation1, R.drawable.image1),
            Affirmation(R.string.affirmation2, R.drawable.image2),
            Affirmation(R.string.affirmation3, R.drawable.image3),
            Affirmation(R.string.affirmation4, R.drawable.image4),
            Affirmation(R.string.affirmation5, R.drawable.image5),
            Affirmation(R.string.affirmation6, R.drawable.image6),
            Affirmation(R.string.affirmation7, R.drawable.image7),
            Affirmation(R.string.affirmation8, R.drawable.image8),
            Affirmation(R.string.affirmation9, R.drawable.image9),
            Affirmation(R.string.affirmation10, R.drawable.image10)
        )
    }

    fun loadWeChatMsg(): List<WeChatMsg> {
        return listOf<WeChatMsg>(
            WeChatMsg(R.drawable.image1, "张总", "项目进度需加快", "15:30"),
            WeChatMsg(R.drawable.image1,"产品组", "周五评审需求", "14:10"),
            WeChatMsg(R.drawable.image1, "全家群", "国庆节去旅游", "10:05"),
            WeChatMsg(R.drawable.image1, "游戏队友", "今晚开黑", "昨天")
        )
    }

    fun loadEntryData(): List<EntryBean> {
        return listOf<EntryBean>(
            EntryBean(
                "BirthdayCardActivity",
                "学习文本、图片组件的基本用法",
                "2025年10月12日",
                Class.forName("com.example.androidlearing2423.ui.theme.act.ui.BirthdayCardActivity")
            ),
            EntryBean(
                "BusinessCardActivity",
                "学习图标资源以及Column和Row容器的使用",
                "2025年10月12日",
                Class.forName("com.example.androidlearing2423.ui.theme.act.ui.BusinessCardActivity")
            ),
            EntryBean(
                "ListCardActivity",
                "学习列表组件的使用",
                "2025年10月12日",
                Class.forName("com.example.androidlearing2423.ui.theme.act.ui.ListCardActivity")
            ),
            EntryBean(
                "LoginActivity",
                "基础组件综合实训，登录界面",
                "2025年10月12日",
                Class.forName("com.example.androidlearing2423.ui.theme.act.ui.LoginActivity")
            ),
            EntryBean(
                "RollDiceActivity",
                "摇塞子综合案例",
                "2025年10月12日",
                Class.forName("com.example.androidlearing2423.ui.theme.act.ui.RollDiceActivity")
            ),
            EntryBean(
                "TipCalculatorActivity",
                "小费计算器综合案例",
                "2025年10月12日",
                Class.forName("com.example.androidlearing2423.ui.theme.act.ui.TipCalculatorActivity")
            ),
            EntryBean(
                "WeChatActivity",
                "列表组件综合实训，微信聊天",
                "2025年10月12日",
                Class.forName("com.example.androidlearing2423.ui.theme.act.ui.WeChatActivity")
            )
        )
    }
}

// 补充Affirmation类定义（添加注解依赖）
data class Affirmation(
    @StringRes val stringResourceId: Int,
    @DrawableRes val imageResourceId: Int
)

class MainActivity : ComponentActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContent {
            AndroidLearing2423Theme { // 修正主题类名（原拼写错误：AndroidLearing→AndroidLearning）
                Scaffold(
                    modifier = Modifier.fillMaxSize(),
                    topBar = {
                        Box(
                            modifier = Modifier
                                .fillMaxWidth()
                                .height(56.dp)
                                .background(Color(0xFFEDEDED)),
                            contentAlignment = Alignment.Center
                        ) {
                            Text(
                                text = "学习列表",
                                fontSize = 20.sp,
                                fontWeight = FontWeight.Bold
                            )
                        }
                    }
                ) { innerPadding ->
                    EntryList(
                        entries = Datasource().loadEntryData(),
                        modifier = Modifier
                            .padding(innerPadding)
                            .fillMaxSize()
                    )
                }
            }
        }
    }
}

@Composable
fun EntryList(
    entries: List<EntryBean>,
    modifier: Modifier = Modifier
) {
    LazyColumn(modifier = modifier) {
        items(entries) { entry ->
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(16.dp),
                verticalAlignment = Alignment.CenterVertically
            ) {
                Box(
                    modifier = Modifier
                        .size(40.dp)
                        .background(Color.Gray)
                        .clip(RoundedCornerShape(4.dp))
                )
                Spacer(modifier = Modifier.width(12.dp))
                Column {
                    Text(text = entry.activityName, fontSize = 16.sp, fontWeight = FontWeight.Medium)
                    Text(text = entry.description, fontSize = 14.sp, color = Color.Gray)
                    Text(text = entry.date, fontSize = 12.sp, color = Color.DarkGray)
                }
            }
            Box(
                modifier = Modifier
                    .fillMaxWidth()
                    .height(0.5.dp)
                    .background(Color(0xFFEEEEEE))
                    .padding(horizontal = 16.dp)
            )
        }
    }
}

@Preview(showBackground = true)
@Composable
fun EntryListPreview() {
    AndroidLearing2423Theme { // 修正主题类名
        EntryList(
            entries = listOf(
                EntryBean(
                    "TestActivity",
                    "测试描述",
                    "2025-01-01",
                    MainActivity::class.java
                )
            )
        )
    }
}


